package com.sangeng.runner;

import com.sangeng.domain.entity.Article;
import com.sangeng.service.ArticleService;
import com.sangeng.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class ViewCountRunner implements CommandLineRunner {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private RedisCache redisCache;

    @Override
    public void run(String... args) throws Exception {
        // 查询博客信息
        List<Article> articleList = articleService.list();
        Map<String, Integer> viewCountMap = articleList.stream().collect(Collectors.toMap(
                article -> article.getId().toString(),
                article -> article.getViewCount().intValue()
        ));
        // 存储到redis中
        // 存储到redis中
        redisCache.setCacheMap("article:viewCount", viewCountMap);
        System.out.println("=========================viewCount初始化完成=========================");
    }
}
